<?php

/*
info: 商区分类管理
date: 2011-11-23
*/

if (!defined('WEBINROOT'))
{
	header("HTTP/1.0 404 Not Found");
	exit();
}

class AreaCategoryDataModel extends CategoryDataModel {

	protected $categoryItemClass='AreaCategoryItem';
	protected $cachekey = 'category_area';

	protected $tableName = 'area';
	protected $title = '商圈';

	protected $supportCheckRelation = true;
	protected $relationTableName = 'company'; //关联表
	protected $relationField = 'area_id'; //关联字段
	protected $relationErrorMessage = '该商圈下还有商家信息, 不能删除.'; //关联表错误提示信息

	public function formatCategoryOption($categoryId, $layer = 0, $containSelf = true) {

		$tempResult = array();
		$tempChildClass = $this->getChildForLayer($categoryId, $layer, $containSelf);
		foreach ($tempChildClass as $tempkey => $tempvalue) {
			$tempComma = $tempvalue['layer'] == 1 ? '' : "├";
			$tempvalue['showName'] = str_repeat("&nbsp;&nbsp;", $tempvalue['layer'] - 1). $tempComma .$tempvalue['name'];
			$tempvalue['levelname'] = $this->getLevelTypeName($tempvalue['level']);
			$tempvalue['tdclass'] = $tempvalue['layer'] == 1 ? ' class="evenrow"' : '';
			$tempResult[$tempkey] = $tempvalue;
		}
		return $tempResult;
	}

	protected function getLevelTypeName($type) {
		$levelType = $this->areaLevelTypeMap();

		return isset($levelType[$type]) ? $levelType[$type] : '';
	}

	//获取某一类型的全部分类(level-- 0:省 1:市 2:区 3:街道)
	public function getCategoryForLevel($level = 0) {
		$tempReturn = array();
		if ($items = $this->getData()) {
			foreach ($items as $key => $value) {
				if ($value['level'] == $level) {
					$tempReturn[$key] = $value;
				}
			}
		}
		return $tempReturn;
	}

	public function areaLevelTypeMap() {
		return array(
			0 => '省',
			1 => '市',
			2 => '区',
			3 => '街道'
		);
	}

	public function getItem($id, $sign = 'id') {
		if ($sign == 'id') {
			return parent::getItem($id);
		} elseif ($sign == 'sign') {
			return $this->getItemBySign($id);
		}

		return array();
	}

	public function getItemBySign($sign) {
		if ($this->useCache) {
			$items = $this->getData();
			foreach ($items as $key => $value) {
				if ($value['short_english'] == $sign) {
					return $value;
				}
			}
		} else {
			return $this->db->GetOne("SELECT * FROM {$this->webconfig['dbpre']}{$this->tableName} WHERE short_english = '{$sign}' LIMIT 0, 1");
		}

		return false;
	}

	public function checkSignForEdit($sign, $editid) {
		if ($this->useCache) {
			$items = $this->getData();
			foreach ($items as $key => $value) {
				if ($value['short_english'] == $sign && $value['id'] != $editid) {
					return true;
				}
			}
		} else {
			return $this->db->GetOne("SELECT * FROM {$this->webconfig['dbpre']}{$this->tableName} WHERE id != '{$editid}' AND short_english = '{$sign}' LIMIT 0, 1");
		}

		return false;
	}
}

class AreaCategoryItem extends CategoryItem {
	protected $shortEnglish;
	protected $level;

	public function __construct($args) {
		parent::__construct($args);

		if (isset($args['short_english'])) {
			$this->setShortEnglish($args['short_english']);
		}

		if (isset($args['level'])) {
			$this->setLevel($args['level']);
		}
	}

	public function setShortEnglish($name) {
		$this->shortEnglish = $name;
	}

	public function getShortEnglish() {
		return $this->shortEnglish;
	}

	public function setLevel($level) {
		$this->level = $level;
	}

	public function getLevel() {
		return $this->level;
	}

	public function toArray() {
		$result = parent::toArray();

		$result['short_english'] = $this->getShortEnglish();
		$result['level'] = $this->getLevel();

		return $result;
	}
}